import { Provide } from '@midwayjs/core';
import { BaseService } from '@cool-midway/core';
import { InjectEntityModel } from '@midwayjs/typeorm';
import { Repository } from 'typeorm';
import { PayConfigEntity } from '../entity/config';

/**
 * 支付配置
 */
@Provide()
export class PayConfigService extends BaseService {
  @InjectEntityModel(PayConfigEntity)
  payConfigEntity: Repository<PayConfigEntity>;

  async update(param: any) {
    console.log(param, 'param')
    if (param.baseStatus == 1) {
      await this.payConfigEntity.createQueryBuilder()
        .where("id != :id", { id:param.id })
        .update({
          baseStatus: 0
        })
        .execute();
    }else{
      let s = await  this.payConfigEntity.createQueryBuilder()
        .where("id != :id", { id:param.id })
        .andWhere("baseStatus = :baseStatus ",{baseStatus:1})
        .getCount()
      if(s == 0){
        throw new Error("至少有一条默认支付")
      }
    }
    return await super.update(param);
  }
}
